﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;

namespace SHomeWorkshop.LunarPresentation.Adorners
{
    /// <summary>
    /// 1－4级标题文本块专用。
    /// 且只在垂直子面板中、标题未被折叠时才有效。
    /// </summary>
    public class TreeLineAdorner : Adorner
    {
        /// <summary>
        /// 用以绘制树型连线的装饰器。
        /// </summary>
        /// <param name="adornedElement">应传入公式的“MainBorder”主子元素。</param>
        /// <param name="masterBasetext">宿主公式，装饰器就是表示这个公式的被选取状态。</param>
        public TreeLineAdorner(UIElement adornedElement, Expressions.BaseText masterBasetext)
            : base(adornedElement)
        {
            this.masterBaseText = masterBasetext;

            //终于解决了选取装饰器中线条宽度为2而不是1像素的问题！！！
            this.SnapsToDevicePixels = true;
            this.SetValue(RenderOptions.EdgeModeProperty, EdgeMode.Aliased);
        }

        private Expressions.BaseText masterBaseText;

        public Expressions.BaseText MasterBaseText
        {
            get { return masterBaseText; }
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            // 即使ShowTreeLine属性为真也不一定绘制。
            if (masterBaseText == null || masterBaseText.ShowTreeLine == false) return;
            if (masterBaseText.ParentPanel == null) return;
            if (masterBaseText.ParentPanel.Direction == Orientation.Horizontal) return;
            if (masterBaseText.IsHeaderText == false) return;
            if (masterBaseText.Indent > 3) return;// isHeaderText为真时，缩进可能并不在[０，３]这四级。
            if (masterBaseText.IsCollapsed) return;

            // 没有“下级”公式。无须绘制树型连线。
            List<Expressions.Expression> subExs = masterBaseText.GetSubIndentExpressions();
            if (subExs == null || subExs.Count <= 0) return;

            Rect adornedElementRect = new Rect(this.AdornedElement.RenderSize);

            double width = masterBaseText.Editor.ExFontSize;

            Brush renderBrush = masterBaseText.ExForeground;
            Pen renderPen = new Pen(masterBaseText.ExForeground, 1);

            renderPen.EndLineCap = PenLineCap.Square;
            renderPen.DashStyle = DashStyles.Solid;

            double lastImmediateSubExY = 0;

            Point startPt = new Point(adornedElementRect.BottomLeft.X + width,
                adornedElementRect.BottomLeft.Y);
            Point basePt = new Point(0, 0);

            for (int i = 0; i < subExs.Count; i++)
            {
                Expressions.Expression subEx = subExs[i];

                if (masterBaseText.Indent < 3)
                {
                    if (subEx.Indent != masterBaseText.Indent + 1) continue;//只绘制直接下级的连线。
                }

                Point subExTopLeftPt = subEx.TranslatePoint(basePt, masterBaseText);
                double y = subEx.ActualHeight / 2 + subExTopLeftPt.Y - masterBaseText.ActualHeight;

                Point subExEndPt = new Point(subExTopLeftPt.X, startPt.Y + y);

                drawingContext.DrawLine(renderPen,
                    new Point(startPt.X, subExEndPt.Y), subExEndPt);

                lastImmediateSubExY = subExEndPt.Y;
            }

            if (lastImmediateSubExY > 0)
            {
                drawingContext.DrawLine(renderPen, startPt,
                    new Point(startPt.X, lastImmediateSubExY));
            }
        }
    }
}